package com.kkd.kkdapp.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.db.app.model.User;
import com.kkd.kkdapp.http.entity.AccountInfoRes;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.patternlock.PatternLockUtils;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;

/*******
 * 
 * @author mty
 * 
 * @Name 密码修改
 */
public class ModifyPassWordActivity extends BaseActivity implements
		OnClickListener {

	private static final int SET_TRADINGPSWORD_OK = 1;
	private static final int SET_TRADINGPSWORD_NO = 2;
	private static final int SUCCESS_REQUEST = 1001;
	private static final int FAIL_REQUEST = 1002;
	private RelativeLayout top_img; // 返回
	private RelativeLayout modify_login_password; // 登录密码修改
	private int isPayPassword;
	private RelativeLayout modify_gesture_password; // 修改手势密码
	private boolean confirmStarted = false;
	private AccountInfoRes data;
	private boolean FLAG_CANREQUSET = true;
	private String url_updateSinaPay = "";
	private Dialog loadDialog;
	private TextView embody_password;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCCESS_REQUEST:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				data = (AccountInfoRes) msg.obj;
				if (data.getWithholdAuth().getWithdrawStatus() == 1) { // 走新浪支付
					password_sinapay.setVisibility(View.VISIBLE);
					enchashment_ll.setVisibility(View.GONE);
					if (data.getWithholdAuth().getIsWithholdAuth() == 0) {
						update_password_sinapay.setText("设置");
					} else {
						update_password_sinapay.setText("修改");
						// 请求修改新浪支付密码接口
						if (FLAG_CANREQUSET) {
							User user = CurUserModel.getCurUser();
							if (user == null)
								return;
							FLAG_CANREQUSET = false;
							KkdHttpMagager.getInstance().updateSinaPayPassword(
									new HttpCallBack() {

										@Override
										public void onSuccess(int statusCode,
												Object responseBody) {
											url_updateSinaPay = (String) responseBody;
											FLAG_CANREQUSET = true;
										}

										@Override
										public void onFailure(int statusCode,
												String desc) {
											FLAG_CANREQUSET = true;
										}
									});
						}
					}
				} else { // 走旧的提现接口
					password_sinapay.setVisibility(View.GONE);
					enchashment_ll.setVisibility(View.VISIBLE);
					requestIsSetPayPassword();
					if (PreferenceUtils.getInstance().getWithdrawPassWord(
							ModifyPassWordActivity.this) == 1) {
						embody_password
								.setText(R.string.accountSettingActivity_updatepssword);
					} else {
						embody_password
								.setText(R.string.accountSettingActivity_setpssword);
					}
				}
				break;
			case FAIL_REQUEST:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				AppUtil.makeToastForCustomer(ModifyPassWordActivity.this,
						(String) msg.obj);
				AppUtil.stopProgressDialog();
				break;

			case SET_TRADINGPSWORD_OK:
				if (isPayPassword != PreferenceUtils.getInstance()
						.getWithdrawPassWord(ModifyPassWordActivity.this)) {
					if (1 == isPayPassword) {
						embody_password
								.setText(R.string.accountSettingActivity_updatepssword);
						PreferenceUtils.getInstance().setWithdrawPassWord(
								ModifyPassWordActivity.this, 1);
					} else if (0 == isPayPassword) {
						PreferenceUtils.getInstance().setWithdrawPassWord(
								ModifyPassWordActivity.this, 0);
						embody_password
								.setText(R.string.accountSettingActivity_setpssword);
					}
				}
				break;

			case SET_TRADINGPSWORD_NO:
				AppUtil.stopProgressDialog();
				break;
			}
		};
	};
	private View password_sinapay;
	private View detail_password_sinapay;
	private View enchashment_ll;
	private TextView update_password_sinapay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_pass_word);
		initView();
		setListener();
	}

	/*************
	 * 初始化控件
	 *************/
	public void initView() {
		top_img = (RelativeLayout) this.findViewById(R.id.top_img);
		modify_login_password = (RelativeLayout) this
				.findViewById(R.id.modify_login_password);
		modify_gesture_password = (RelativeLayout) this
				.findViewById(R.id.modify_gesture_password);
		password_sinapay = findViewById(R.id.password_sinapay);
		detail_password_sinapay = findViewById(R.id.detail_password_sinapay);
		enchashment_ll = findViewById(R.id.enchashment_ll);// 旧提现密码
		embody_password = (TextView) this.findViewById(R.id.embody_password);
		update_password_sinapay = (TextView) findViewById(R.id.update_password_sinapay);
	}

	/*************
	 * 注册监听事件
	 *************/
	public void setListener() {
		top_img.setOnClickListener(this);
		modify_login_password.setOnClickListener(this);
		modify_gesture_password.setOnClickListener(this);
		detail_password_sinapay.setOnClickListener(this);
		enchashment_ll.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!"".equals(embody_password.getText().toString())) {
					if ("修改".equals(embody_password.getText().toString())) {
						intent = new Intent(ModifyPassWordActivity.this,
								WithdrawUpdateActivity.class);
						startActivity(intent);
					} else if ("设置"
							.equals(embody_password.getText().toString())) {
						intent = new Intent(ModifyPassWordActivity.this,
								SetDealActivity.class);
						startActivity(intent);
					}

				}
			}
		});
		password_sinapay.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {// 修改新浪支付密码
				if (data != null) {
					if (data.getWithholdAuth().getIsWithholdAuth() == 0) {// 未设置过新浪支付密码
						intent = new Intent(ModifyPassWordActivity.this,
								KKDSinaWebActivity.class);
						intent.putExtra("url", data.getWithholdAuth()
								.getSinaWithholdAuthUrl());
						startActivity(intent);
					} else {
						if (!TextUtils.isEmpty(url_updateSinaPay)) {
							intent = new Intent(ModifyPassWordActivity.this,
									KKDSinaWebActivity.class);
							intent.putExtra("url", url_updateSinaPay);
							startActivity(intent);
						} else {
							if (FLAG_CANREQUSET) {
								User user = CurUserModel.getCurUser();
								if (user == null)
									return;
								FLAG_CANREQUSET = false;
								KkdHttpMagager.getInstance()
										.updateSinaPayPassword(
												new HttpCallBack() {

													@Override
													public void onSuccess(
															int statusCode,
															Object responseBody) {
														intent = new Intent(
																ModifyPassWordActivity.this,
																KKDSinaWebActivity.class);
														intent.putExtra(
																"url",
																(String) responseBody);
														intent.putExtra(
																"useTitleName",
																true);
														intent.putExtra(
																"titleName",
																"新浪支付");
														startActivity(intent);
														FLAG_CANREQUSET = true;
													}

													@Override
													public void onFailure(
															int statusCode,
															String desc) {
														FLAG_CANREQUSET = true;
													}
												});
							}
						}
					}
				}
			}
		});
	}

	private void requestGetaccountInfo() {
		User user = CurUserModel.getCurUser();
		if (user == null)
			return;
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(this, "加载中...");
		KkdHttpMagager.getInstance().getAccountInfoManager(
				AccountInfoRes.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = Message.obtain();
						msg.what = SUCCESS_REQUEST;
						msg.obj = responseBody;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_REQUEST;
						msg.obj = desc;
						mHandler.sendMessage(msg);
					}
				});
	}

	Intent intent;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_img: // 返回
			finish();
			break;
		case R.id.modify_login_password: // 登录密码修改
			intent = new Intent(this, UpdateLoginActivity.class);
			startActivity(intent);
			break;
		case R.id.modify_gesture_password: // 修改手势密码
			// this.confirmStarted = true;
			// PatternLockUtils.confirmPatternIfHas(this);
			// // 控制手势密码验证提示语
			// PreferenceUtils.getInstance().confirmPatternflag = false;
			// // 控制设置手势密码提示语
			// PreferenceUtils.getInstance().setPatternflag = false;
			// // 忘记收拾密码？ 与 其它账号登录可见与不可见
			// PreferenceUtils.getInstance().setButtonflag = false;
			// // 是否必须验证通过
			// PreferenceUtils.getInstance().isMustRight = false;
			if (TextUtils.isEmpty(PreferenceUtils.getInstance().getGesturePW(
					this))) {
				Intent intent = new Intent(this, GestureEditActivity.class);
				intent.putExtra("update", true);
				startActivity(intent);
			} else {
				Intent intent = new Intent(this, GestureVerifyActivity.class);
				intent.putExtra("update", true);
				startActivity(intent);
			}
			break;

		case R.id.detail_password_sinapay: // 修改新浪支付密码提示
			Toast.makeText(this, "提示", Toast.LENGTH_SHORT).show();
			break;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (PreferenceUtils.getInstance().getLogin(this) == 0) {
			requestGetaccountInfo();
		} else {
			finish();
		}
		TextView patternlock = (TextView) this.findViewById(R.id.patternlock);
		if (TextUtils.isEmpty(PreferenceUtils.getInstance().getGesturePW(this))) {
			patternlock.setText("设置");
		} else {
			patternlock.setText("修改");
		}
		// if (PatternLockUtils.hasPattern(this)) {
		// patternlock.setText("修改");
		// } else {
		// patternlock.setText("设置");
		// }
	}

	/**
	 * 
	 * 判断是否设置交易密码
	 */
	public void requestIsSetPayPassword() {
		KkdHttpMagager.getInstance().getWithdrawUpdateManager(String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						try {
							String responseString = responseBody.toString();
							JSONObject data = new JSONObject(responseString);
							isPayPassword = data.getInt("isPayPassword");
							mHandler.sendEmptyMessage(SET_TRADINGPSWORD_OK);
						} catch (JSONException e) {

							e.printStackTrace();
						}
						mHandler.sendEmptyMessage(SET_TRADINGPSWORD_NO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						mHandler.sendEmptyMessage(SET_TRADINGPSWORD_NO);
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 手势密码控制
		// settingPatternLock(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}
}
